from django.contrib import admin
from django.utils.html import format_html
from django.urls import reverse
from django.utils.safestring import mark_safe
from .models import SharedNote, SharedFolder, ShareComment, ShareAccess, CommentLike


@admin.register(SharedNote)
class SharedNoteAdmin(admin.ModelAdmin):
    """
    共享笔记管理界面
    """
    list_display = (
        'id', 'title', 'shared_by', 'permission', 'scope', 
        'is_active', 'created_at'
    )
    list_filter = ('permission', 'scope', 'is_active', 'created_at')
    search_fields = ('title', 'description', 'shared_by__username', 'shared_by__email')
    readonly_fields = ('id', 'share_token', 'created_at', 'updated_at')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'description', 'note', 'shared_by')
        }),
        ('权限设置', {
            'fields': ('permission', 'scope', 'password')
        }),
        ('访问控制', {
            'fields': ('expires_at', 'is_active')
        }),
        ('统计信息', {
            'fields': ('share_token', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
    
    def get_queryset(self, request):
        """优化查询性能"""
        return super().get_queryset(request).select_related('shared_by', 'note')


@admin.register(SharedFolder)
class SharedFolderAdmin(admin.ModelAdmin):
    """
    共享文件夹管理界面
    """
    list_display = (
        'id', 'title', 'shared_by', 'permission', 'scope', 
        'is_active', 'created_at'
    )
    list_filter = ('permission', 'scope', 'is_active', 'created_at')
    search_fields = ('title', 'description', 'shared_by__username', 'shared_by__email')
    readonly_fields = ('id', 'share_token', 'created_at', 'updated_at')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'description', 'category', 'shared_by')
        }),
        ('权限设置', {
            'fields': ('permission', 'scope', 'password')
        }),
        ('访问控制', {
            'fields': ('expires_at', 'is_active')
        }),
        ('统计信息', {
            'fields': ('share_token', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
    
    def get_queryset(self, request):
        """优化查询性能"""
        return super().get_queryset(request).select_related('shared_by', 'folder')


@admin.register(ShareComment)
class ShareCommentAdmin(admin.ModelAdmin):
    """
    分享评论管理界面
    """
    list_display = (
        'id', 'content_short', 'author', 'shared_note', 
        'like_count', 'is_approved', 'created_at'
    )
    list_filter = ('is_approved', 'is_deleted', 'created_at')
    search_fields = ('content', 'author__username', 'author__email')
    readonly_fields = ('id', 'like_count', 'created_at', 'updated_at')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('评论内容', {
            'fields': ('content', 'author', 'shared_note')
        }),
        ('状态管理', {
            'fields': ('is_approved', 'is_deleted')
        }),
        ('统计信息', {
            'fields': ('like_count', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
    
    def content_short(self, obj):
        """显示简短评论内容"""
        if len(obj.content) > 50:
            return obj.content[:50] + '...'
        return obj.content
    content_short.short_description = '评论内容'
    
    def get_queryset(self, request):
        """优化查询性能"""
        return super().get_queryset(request).select_related('author', 'shared_note')


@admin.register(ShareAccess)
class ShareAccessAdmin(admin.ModelAdmin):
    """
    分享访问记录管理界面
    """
    list_display = (
        'id', 'shared_note', 'visitor', 'ip_address', 
        'user_agent_short', 'created_at'
    )
    list_filter = ('created_at',)
    search_fields = ('visitor__username', 'visitor__email', 'ip_address')
    readonly_fields = ('shared_note', 'visitor', 'ip_address', 'user_agent', 'created_at')
    ordering = ('-created_at',)
    
    def user_agent_short(self, obj):
        """显示简短的用户代理信息"""
        if len(obj.user_agent) > 50:
            return obj.user_agent[:50] + '...'
        return obj.user_agent
    user_agent_short.short_description = '用户代理'
    
    def get_queryset(self, request):
        """优化查询性能"""
        return super().get_queryset(request).select_related('shared_note', 'visitor')
    
    def has_add_permission(self, request):
        """禁止手动添加访问记录"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """禁止修改访问记录"""
        return False


@admin.register(CommentLike)
class CommentLikeAdmin(admin.ModelAdmin):
    """
    评论点赞管理界面
    """
    list_display = ('id', 'comment', 'user', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('user__username', 'user__email', 'comment__content')
    readonly_fields = ('comment', 'user', 'created_at')
    ordering = ('-created_at',)
    
    def get_queryset(self, request):
        """优化查询性能"""
        return super().get_queryset(request).select_related('comment', 'user')
    
    def has_add_permission(self, request):
        """禁止手动添加点赞记录"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """禁止修改点赞记录"""
        return False
